package durst.data.json;

import java.sql.SQLException;

import com.sdicons.json.mapper.JSONMapper;
import com.sdicons.json.mapper.MapperException;
import com.sdicons.json.mapper.helper.SimpleMapperHelper;
import com.sdicons.json.model.JSONValue;

import durst.data.Account;
import durst.data.Invite;
import durst.data.dao.DaoFactory;

public class InviteJSONHelper implements SimpleMapperHelper {

	@Override
	public Class<Invite> getHelpedClass() {
		return Invite.class;
	}

	@Override
	public JSONValue toJSON(Object aPojo) throws MapperException {
		if (aPojo instanceof Invite && aPojo != null) {
			Invite inv = (Invite)aPojo;

			if (inv.getInvitee() == null) { 
				try {
					DaoFactory.getInstance().createDao(Account.class).refresh(inv.getInvitee());
				} catch (SQLException e) {
					throw new MapperException("could not refresh Invitee object from DB!");
				}
			}
				
			return JSONMapper.toJSON(inv.getInvitee());
		}
		
		return null;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Object toJava(JSONValue aValue, Class aRequestedClass)
			throws MapperException {
		// TODO Auto-generated method stub
		return null;
	}

}
